package cmsoa.action.system;


import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cmsoa.db.DBUtil;
import cmsoa.db.pojo.TXtMenu;
import cmsoa.db.pojo.TXtUser;
import cmsoa.helper.RequestHelper;

import com.opensymphony.xwork2.Action;

public class MenuTreeAction {
	private List<Map> trees;
	private TXtMenu tree;
	
	public static void main(String[] args) {
		
		String id = parseMenuId(parseId("xt010009"));
		System.out.println(id);
		
	}
	
	
	public String delete(){
		DBUtil db = DBUtil.getDBUtilByRequest();
		db.delete(this.tree);
		return Action.SUCCESS;
	}
	
	public String save(){
		DBUtil db = DBUtil.getDBUtilByRequest();
		if(this.tree.getMenuId()==null || "".equals(this.tree.getMenuId().trim())){
			String sql = "select menu_Id from t_xt_menu where menu_Parent_Id=? order by menu_Id desc ";
			List params = new ArrayList(1);
			params.add(this.tree.getMenuParentId());
			List<Map> list = db.queryBySQL(sql, params,0,1);
			if(list.size()>0){
				String maxId = String.valueOf(list.get(0).get("menuId"));
				String id = parseMenuId(parseId(maxId));
				this.tree.setMenuId(id);
			}else{
				if(this.tree.getMenuParentId().length()<=4){
					this.tree.setMenuId(this.tree.getMenuParentId()+"01");
				}else{
					this.tree.setMenuId(this.tree.getMenuParentId()+"0001");
				}
			}
			this.tree.setLrSj(new Timestamp(new Date().getTime()));
			db.insert(this.tree);
		}else{
			Map params = new HashMap(1);
			params.put("menuId", tree.getMenuId());
			TXtMenu menu = (TXtMenu)db.queryByPojo(TXtMenu.class, params).get(0);
			menu.setPxXh(tree.getPxXh());
			menu.setUrl(tree.getUrl());
			menu.setMenuName(tree.getMenuName());
			menu.setXgSj(new Timestamp(new Date().getTime()));
			db.update(menu);
		}
		return Action.SUCCESS;
	}
	
	private static String[] parseId(String id){
		String[] result = new String[id.length()/4+1];
		int index = 0;
		for (int i = 0; i < result.length; i++) {
			result[i] = id.substring(index, index+=(i<2?2:4));
		}
		return result;
	}
	
	private static final String[] zeros = new String[]{"","0","00","000","0000","00000","000000","0000000","00000000"};
	private static String parseMenuId(String[] ids){
		String lid = ids[ids.length-1];
		int value = Integer.parseInt(lid)+1;
		StringBuffer id = new StringBuffer();
		for (int i = 0; i < ids.length-1; i++) {
			id.append(ids[i]);
		}
		id.append(zeros[lid.length() - (value+"").length()]);
		id.append(value);
		return id.toString();
	}
	
	private static final String SQL_MENU = "select * from t_xt_menu order by menu_id, px_xh";
 
	public String menuTree(){
		DBUtil db = DBUtil.getDBUtilByRequest();
		TXtUser currUser = (TXtUser)RequestHelper.getSession().getAttribute("user");
		List<Map> list = db.queryBySQL(SQL_MENU);
		Map<String,Map> temp = new HashMap(list.size());
		for (Map pojo : list) {
			Map node = new HashMap(5);
			node.put("id", pojo.get("menuId"));
			node.put("text", pojo.get("menuName"));
			node.put("parentId", pojo.get("menuParentId"));
			Map temp1 = new HashMap(2);
			temp1.put("pxXh", pojo.get("pxXh"));
			if(pojo.get("url")!=null && pojo.get("url").toString().length()!=0){
				temp1.put("url", pojo.get("url"));
			}else{
				temp1.put("url", "");
				node.put("state", "closed");
			}
			node.put("attributes", temp1);
			temp.put(pojo.get("menuId").toString(), node);
		}
		
		Map root = null;
		for (Map pojo : list) {
			Map node = temp.get(pojo.get("menuId"));
			if("root".equals(node.get("parentId"))){
				root = node;
			}else{
				Map parentMap = temp.get(node.get("parentId"));
				if(parentMap!=null){
					if(parentMap.get("children")==null){
						parentMap.put("children", new ArrayList());
					}
					((ArrayList)parentMap.get("children")).add(node);
				}
			}
		}
		
		trees = new ArrayList(1);
		if(root!=null){
			trees.add(root);
		}
		return Action.SUCCESS;
	}

	public List getTrees() {
		return trees;
	}

	public TXtMenu getTree() {
		return tree;
	}

	public void setTree(TXtMenu tree) {
		this.tree = tree;
	}
	
	
}
